package com.appheader.rongmaphone.utils.network;

import android.text.TextUtils;
import android.util.Log;

import com.appheader.rongmaphone.cache.preference.PreferenceUtil;
import com.appheader.rongmaphone.constants.GlobalVars;
import com.appheader.rongmaphone.user.UserInfo;
import com.appheader.rongmaphone.utils.AES;
import com.appheader.rongmaphone.utils.MD5;
import com.appheader.rongmaphone.utils.json.JSONUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * Created by zhep on 2016/10/10.
 */
public class HttpHeader {
    private static String session_id;
    private static String uid;

    public static String generateSecureStr(String url,
                                           Map<String, String> params) {
        int base = (int) (Math.random() * 100000);
        String numberStr = String.valueOf(base * 7);
        return AES
                .encrypt((url + "|" + generateParamsMD5(params) + "|" + numberStr)
                        .getBytes());
    }
    /**
     * 网络请求的url
     * @param url
     * @param
     * @return
     */
    public static Request getHeaderZipRequest(String url){
        PreferenceUtil preferenceUtil = new PreferenceUtil(GlobalVars.getContext());
        boolean isFirst = preferenceUtil.getBoolean("isFirst");
        String userStr = preferenceUtil.getString("userStr");
        Log.e("TAG", "getHeaderRequest: "+isFirst+"++++++"+userStr+"-------");

        if (!TextUtils.isEmpty(userStr)) {
            UserInfo userInfos = JSONUtil.userInfos(userStr);
            session_id = userInfos.getSession_id();

//            Log.e("TAG", "getHeaderRequest: "+session_id);
        }
        return new Request.Builder()
                .addHeader("platform", "android")
                .addHeader("versionCode", String.valueOf(GlobalVars.getVersionCode()))
                .addHeader("clientType", "customer")
                .addHeader("session_id",session_id)
                .get()
                .url(url)
                .build();

    }
    //okHttp实现post请求
    public  static Request getHeaderRequest(String url,
                                           Map<String, String> params) {
        PreferenceUtil preferenceUtil = new PreferenceUtil(GlobalVars.getContext());
           boolean isFirst = preferenceUtil.getBoolean("isFirst");
        String userStr = preferenceUtil.getString("userStr");

//        Log.e("TAG", "getHeaderRequest: "+isFirst+"++++++"+userStr+"-------");


        if (!TextUtils.isEmpty(userStr)) {
            UserInfo userInfos = JSONUtil.userInfos(userStr);
            session_id = userInfos.getSession_id();

//            Log.e("TAG", "getHeaderRequest: "+session_id);
        }
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if (params != null) {
            for (String temp : params.keySet()) {
                formBodyBuilder.add(temp, params.get(temp));

            }
        }

        FormBody formBody = formBodyBuilder.build();
//        UserInfo currentUser = CurrentUserManager.getCurrentUser();

        //判断如果用户不存在就走addHeader不带sessionId的，否则就走addHeader带sessionId

        if (!TextUtils.isEmpty(session_id)) {
//            map.put("session_id", currentUser.getSession_id());
            return new Request.Builder()
                    .addHeader("platform", "android")
                    .addHeader("versionCode", String.valueOf(GlobalVars.getVersionCode()))
                    .addHeader("clientType", "customer")
                    .addHeader("ss", HttpHeader.generateSecureStr(url, params))
                    .addHeader("session_id",session_id)
                    .post(formBody)
                    .url(url)
                    .build();
        } else {

            return new Request.Builder()
                    .addHeader("platform", "android")
                    .addHeader("versionCode", String.valueOf(GlobalVars.getVersionCode()))
                    .addHeader("clientType", "customer")
                    .addHeader("ss", HttpHeader.generateSecureStr(url, params))
                    .post(formBody)
                    .url(url)
                    .build();
        }

    }


    private static String generateParamsMD5(Map<String, String> params) {
        StringBuffer paramsStr = new StringBuffer();
        if (params == null || params.isEmpty()) {
        } else {
            List<String> pNameList = new ArrayList<>();
            pNameList.addAll(params.keySet());
            Collections.sort(pNameList);
            for (int i = 0; i < pNameList.size(); i++) {
                String pName = pNameList.get(i);
                String pVal = params.get(pName);
                paramsStr.append(pName).append("=").append(pVal);
                if (i != pNameList.size() - 1)
                    paramsStr.append("&");
            }
        }
        return MD5.encrypt(paramsStr.toString());
    }


}
